package com.vote.dao;

import java.util.List;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;

import com.vote.domain.VoteOption;

@DAO(catalog = "vote")
public interface VoteOptionDAO {
	
	public static String TABLE="vote_option";
	
	public static String INSERT_FIELD="option_text,question_id";
	
	public static String SELECT_FIELD=INSERT_FIELD+",option_count,option_id";
	
	/**
	 * 根据问题id获得该问题的所有选项的信息
	 * @param id
	 * @return
	 */
	@SQL("select "+SELECT_FIELD+" from " +TABLE+" where question_id=:id")
	  public List<VoteOption> getOptionListByQuestionId(@SQLParam("id") int id);
	
	/**
	 * 根据选项id获得该选项的信息
	 * @param id
	 * @return
	 */
	@SQL("select "+SELECT_FIELD+" from " +TABLE+"where option_id=:id")
	  public VoteOption getOption(@SQLParam("id") int id);
	
	/**
	 * 插入选项信息
	 * @param option
	 */
	@SQL("insert into "+TABLE+"("+INSERT_FIELD+")values(:model.optionText,:model.questionId)")
	  public void insertOption(@SQLParam("model") VoteOption option);
	
	/**
	 * 删除选项
	 * @param id
	 */
	@SQL("delete from "+TABLE+"where option_id=:id")
	  public void deleteOption(@SQLParam("id") int id);
	
	/**
	 * 获取刚插入option的ID
	 * @return
	 */
	@SQL("select LAST_INSERT_ID()")
	public int getLastInsertId();
	
	/**
	 * 更新问卷count。如果该问卷被回答，count加1
	 * @param id
	 */
	@SQL("update "+TABLE+" set option_count=option_count+1 where option_id=:id")
	public void addCount(@SQLParam("id") int id);
	

}
